package com.hightrast.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hightrast.service.api.baseEntity.exception.ServiceException;
import com.hightrast.service.api.domain.qo.rastRole.AddUserRoleQo;
import com.hightrast.service.api.domain.qo.rastUserRoleAuth.QueryRastUserRoleAuthListQo;
import com.hightrast.service.api.domain.qo.rastUserRoleAuth.UpdateRastUserRoleAuthStatusQo;
import com.hightrast.service.api.entity.RastRole;
import com.hightrast.service.api.entity.RastUserRoleAuth;
import com.hightrast.service.mapper.RastUserRoleAuthMapper;
import com.hightrast.service.service.IRastRoleService;
import com.hightrast.service.service.IRastUserRoleAuthService;
import com.hightrast.service.service.IRastUserRoleService;
import com.hightrast.service.util.BaseEntityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 用户角色授权申请表 服务实现类
 * </p>
 *
 * @author hx
 * @since 2025-01-19
 */
@Service
public class RastUserRoleAuthServiceImpl extends ServiceImpl<RastUserRoleAuthMapper, RastUserRoleAuth> implements IRastUserRoleAuthService {

    @Resource
    IRastUserRoleService userRoleService;
    @Resource
    IRastRoleService roleService;

    /**
     * 该方法只有对应权限人员才能获取
     *
     * 获取申请授权角色用户列表
     * @param qo
     * @return
     */
    @Override
    public List<RastUserRoleAuth> queryUserRoleAuthList(QueryRastUserRoleAuthListQo qo) {
        return list(new LambdaQueryWrapper<RastUserRoleAuth>()
                .eq(Objects.nonNull(qo.getAuthStatus()),RastUserRoleAuth::getAuthStatus,qo.getAuthStatus())
                .like(StringUtils.isNotEmpty(qo.getUserName()),RastUserRoleAuth::getUserName,"%"+qo.getUserName()+"%"));
    }

    /**
     * 该方法只有对应权限人员才能获取
     *
     * 修改用户申请权限状态
     * @param qo
     */
    @Override
    @Transactional
    public void updateUserRoleAuthStatus(UpdateRastUserRoleAuthStatusQo qo) {

        RastRole role = Optional.ofNullable(roleService.getById(qo.getRoleId())).orElseThrow(() -> new ServiceException("Authorization role does not exist"));
        // 修改申请授权角色状态
        update(new LambdaUpdateWrapper<RastUserRoleAuth>()
                .eq(RastUserRoleAuth::getId,qo.getUserAuthId())
                .set(RastUserRoleAuth::getRoleId,qo.getRoleId())
                .set(RastUserRoleAuth::getUserId,qo.getUserId())
                .set(RastUserRoleAuth::getAuthStatus,qo.getAuthStatus()));
        // 新增用户角色关联数据
        AddUserRoleQo userRoleQo = new AddUserRoleQo();
        userRoleQo.setRoleId(qo.getRoleId());
        userRoleQo.setRoleName(role.getRoleName());
        userRoleQo.setUserId(qo.getUserId());
        userRoleService.saveUpdateUserRole(userRoleQo);
    }


    /**
     * 创建申请授权用户角色
     * @param userRoleAuth
     */
    @Override
    public void addUserRoleAuth(RastUserRoleAuth userRoleAuth) {
        if(Objects.isNull(userRoleAuth.getUserId())){
            // 申请授权用户不能为空
            throw new ServiceException("The user requesting authorization cannot be empty");
        }
        BaseEntityUtil.save(userRoleAuth);
        save(userRoleAuth);
    }
}
